import { getList } from '@/apis/commonCrud'
import { API_BASE_URL } from '@/utils/url'
import { getToken } from '@/utils/token'
import dayjs from 'dayjs'
import { getTeachCaseKeys } from '@/utils/tcFolderGlob'

export const teachCaseColumn = [
    {
        label: '封面',
        prop: 'poster',
        type: 'upload',
        listType: 'picture-img',
        width: 100,
        propsHttp: {
            res: 'data[0]',
        },
        headers: {
            Authorization: `Bearer ${getToken()}`,
        },
        action: `${API_BASE_URL}/upload`,
        data: {
            folder: 'teachCase',
        },
    },
    {
        label: '标题',
        prop: 'title',
        search: true,
        span: 17,
        rules: [
            {
                required: true,
                message: '此项必填',
                trigger: 'blur',
            },
        ],
    },
    {
        label: '标识',
        prop: 'key',
        labelTip: '教学案例本地文件夹名',
        type: 'select',
        rules: [
            {
                required: true,
                message: '此项必填',
                trigger: 'blur',
            },
        ],
        dicData: getTeachCaseKeys(true),
        span: 9,
    },
    {
        label: '作者',
        prop: 'userId',
        formatter: (value) => {
            // console.log('value', value)
            if (value.user) {
                return value.user.nickname
            }
        },
        span: 8,
    },
    {
        label: '简介',
        prop: 'description',
        type: 'textarea',
        span: 17,
        overHidden: true,
        formatter: () => '...',
        hide: true,
    },

    {
        label: '发布时间',
        prop: 'publishTime',
        bind: 'publishTime',
        type: 'datetime',
        formatter: (_row, val) => {
            return val ? dayjs(val).format('YYYY-MM-DD HH:mm:ss') : ''
        },
        span: 9,
    },
    {
        label: '是否开启评论',
        prop: 'comment',
        type: 'switch',
        dicData: [
            { label: '关闭', value: 0 },
            { label: '开启', value: 1 },
        ],
        value: 0,
        span: 8,
    },
    {
        label: '文档',
        prop: 'doc',
        type: 'textarea',
        span: 24,
        overHidden: true,
        hide: true,
        formatter: () => '...',
    },
    {
        label: '知识库',
        prop: 'knowledgeBaseId',
        type: 'table',
        multiple: false,
        children: {
            column: [
                {
                    label: 'id',
                    prop: 'id',
                },
                {
                    label: '标题',
                    prop: 'title',
                    search: true,
                },
                {
                    label: 'collection名称',
                    prop: 'collectionName',
                },
            ],
        },
        onLoad: async ({ page, value, data }, callback) => {
            if (value && value !== 'add') {
                const res = await getList('knowledgeBases', {
                    id: value,
                })
                if (res.success) {
                    callback(res.data.rows[0])
                }
                return
            }
            if (!page) return
            const res = await getList('knowledgeBases', {
                ...data,
                page: page.currentPage,
                pageSize: 10,
            })
            if (res.success) {
                callback({
                    total: res.data.count,
                    data: res.data.rows,
                })
            }
        },
        props: {
            label: 'title',
            value: 'id',
        },
        span: 17,
        formatter: (value) => {
            if (value.knowledgeBase) {
                return value.knowledgeBase.title
            }
            if (value.knowledgeBase !== null && value.title) {
                return value.title
            }
        },
    },
]
